package com.sst.social.networking.database;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;

public class ImageHandling {

	Resources res;
	Activity activity;

	public ImageHandling(Activity a) {
		activity = a;
		res = a.getResources();
	}

	private Bitmap getBitmapFromResource(int resourceId) {

		Bitmap bitmap = null;
		bitmap = BitmapFactory.decodeResource(res, resourceId);
		return bitmap;
	}

	public byte[] getBitmapAsByteArray(int resourceId) {
		ByteArrayOutputStream outputStream = null;
		byte[] bitmapByteArray = null;
		try {
			outputStream = new ByteArrayOutputStream();
			Bitmap bitmap = getBitmapFromResource(resourceId);
			bitmap.compress(CompressFormat.PNG, 0, outputStream);
			bitmapByteArray = outputStream.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.flush();
					outputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}

		return bitmapByteArray;
	}
}
